﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class PersonalSettingService : IPersonalSettingService
    {
        private readonly IPersonalSettingRepository _personalSettingRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<PersonalSettingService> _logger;

        public PersonalSettingService(IPersonalSettingRepository personalSettingRepository, IMapper mapper, ILogger<PersonalSettingService> logger)
        {
            _personalSettingRepository = personalSettingRepository;
            _mapper = mapper;
            _logger = logger;
        }
        /// <summary>
        /// 获取用户的个人信息
        /// </summary>
        /// <param name="uId">用户Id</param>
        /// <returns></returns>
        public PersonalDetailsOutput GetPersonalDetails(int uId)
        {
            try
            {
                //获取用户的个人信息
                var details = _personalSettingRepository.GetPersonalDetails(uId);
                //将用户的个人信息转换为输出对象
                var personalDetails = _mapper.Map<PersonalDetailsOutput>(details);
                //返回输出对象
                return personalDetails;
            }
            catch (Exception ex)
            {
                //记录日志
                _logger.LogError("获取用户的个人信息失败：" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 更新用户个人信息密码
        /// </summary>
        /// <param name="oldPassword">旧密码</param>
        /// <param name="newPassword">新密码</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdatePassWord(string newPassword, string oldPassword)
        {
            try
            {
                //更新用户密码
                var result = await _personalSettingRepository.UpdatePassWord(newPassword, oldPassword);
                //返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("更新用户个人信息密码失败：" + ex.Message);
                throw;
            }
        }
    }
}
